<template>
  <div class="dynamic-container fullHeight">
    <div class="fullHeight setFlexBox">
      <dynamicSearch ref="dySearch" :searchData="dynamicData.searchData" @postForm="getList" @resetSearch="getRetrieve"
        :isFloatRight="true" style="margin-bottom: 5px;"></dynamicSearch>
      <queryDynamicList ref="dyList" :viewData="dynamicData.viewData" :queryParams="queryParams" class="dynamicList"
        :permissionPrefix="permissionPrefix" :ctUrl="selectedRow.functionData.ctUrl"></queryDynamicList>
    </div>
  </div>
</template>

<script>
import queryDynamicList from '@/components/SpsPublic/Dynamic/queryDynamicList'
import dynamicSearch from '@/components/SpsPublic/Dynamic/dynamicSearch'
import dynamicButton from '@/components/SpsPublic/Dynamic/dynamicButton'
import dynamicAddForm from '@/components/SpsPublic/Dynamic/dynamicAddForm'
import spsMaster from '@/components/SpsPublic/Master/SpsMaster'
import { baseListRetrieveCondition } from '@/api/mis/retrievecondition'
import { listMisView } from '@/components/SpsPublic/Dynamic/misView.js'
import { getBaseFunction } from '@/api/mis/function'
import store from '@/store/index'
import { getFunNo } from '@/utils/party'

export default {
  // name: 'queryDynamic' + this.$route.name.slice(-4),
  name: 'QueryDynamic',
  components: {
    queryDynamicList,
    dynamicSearch,
    dynamicButton,
    dynamicAddForm,
    spsMaster,
  },
  computed: {},
  data() {
    return {
      permissionPrefix: 'dynamic:button:',
      store: store,
      timer: 0,
      dynamicData: {
        searchData: [],
        viewData: [],
        buttonData: {
          add: true,
          update: true,
          delete: true,
        },
        updateData: {},
        handleType: 'view',
        conType: {
          datePicker: 'el-date-picker',
          treeSelect: 'mis-select-tree',
        },
      },
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        queryType: 'Q',
      },
      selectedRow: {
        selectedCt: null,
        functionData: {},
      },
      funNo: null,
      dispSearchData: null,
      routeQuery: null,
    }
  },
  created() {
    this.init()
  },
  activated() {
    if (JSON.stringify(this.routeQuery) !== JSON.stringify(this.$route.query)) {
      this.init()
    }
  },
  methods: {
    init() {
      if (this.childFunNo == undefined || this.childFunNo == null) {
        this.getParam()
      } else {
        this.isChild = true
        this.funNo = this.childFunNo
      }
      this.selectedRow.funNo = this.funNo
      this.queryParams.ctFunNo = this.funNo
      this.routeQuery = this.$route.query
      // 请求获取搜索查询信息数据
      this.getRetrieve()
      // 获取table展示列数据
      this.getMisView()
    },
    // 获取funNo、bussId
    getParam() {
      // 非子表信息，从URL参数中获取
      this.funNo = this.$route.query.funNo
      this.bussId = this.$route.query.bussId
    },
    // 获取URL上的参数
    getQueryVariable(variable) {
      return this.$route.query[variable]
    },
    // 获取该功能的数据
    getFunctionData() {
      const promise = new Promise((resolve, reject) => {
        getBaseFunction(this.funNo).then((response) => {
          this.selectedRow.functionData = response.data
          const pageSize = response.data.amRow
          if (pageSize && pageSize > 0 && pageSize <= 100) {
            this.queryParams.pageSize = pageSize
          }
          resolve()
        })
      })
      const that = this
      promise.then(() => {
        that.$refs.dySearch.postForm()
      })
    },
    // 获取表格显示列
    getMisView() {
      this.loading = true
      listMisView(this.queryParams).then((response) => {
        this.dynamicData.viewData = response.rows
        this.dynamicData.viewData = this.dynamicData.viewData.filter(
          (column) => {
            return column.ctIfTable == '1'
          }
        )
        this.loading = false
      })
    },
    // 获取搜索查询信息数据
    getRetrieve() {
      const that = this
      baseListRetrieveCondition(this.queryParams).then((response) => {
        that.dynamicData.searchData = response.rows
        // 包含下级元素的位置
        let index = -1
        let checkValue = true
        for (let i = 0; i < that.dynamicData.searchData.length; i++) {
          const item = this.dynamicData.searchData[i]
          // 下拉树初始值赋值
          if (
            item.ctConType == that.dynamicData.conType.treeSelect &&
            item.ctConTreetype == '99'
          ) {
            checkValue = item.ctColDefaultv == '1'
            //  组织的下拉查询，赋登录人的管辖范围初始值
            item.ctColDefaultv = store.state.user.branchInfo.manageBranchLevel
            index = i + 1
          }

          // 不显示的查询条件，赋初始值
          if (!item.ctConDisp) {
            const variable = that.getQueryVariable(item.ctColName)
            if (variable && variable != null && variable != '') {
              item.ctColDefaultv = variable
            } else {
              item.ctColDefaultv = that.bussId
            }
          }
        }
        if (index != -1) {
          const retrieve = {
            ctConType: 'el-checkbox',
            ctColDefaultv: checkValue,
            ctColName: 'includeFlag',
            ctColTitle: '',
          }
          this.dynamicData.searchData.splice(index, 0, retrieve)
        }
        // 获取该功能的数据
        this.getFunctionData()
      })
    },
    // 查询表格数据
    getList(val) {
      var keys = Object.keys(val)
      for (var i = 0; i < keys.length; i++) {
        const key = keys[i]
        this.queryParams[key] = val[key]
      }
      this.$refs['dyList'].getList()
    },
  },
}
</script>

<style scoped lang="scss">
.dynamicSearch {
  overflow: visible;
}

.dynamicList {
  position: relative;
}

.floatLeft .el-form--inline {
  float: left;
}

.floatRight .el-form--inline {
  float: right;
}
</style>
